package com.bq.server.base;

import com.google.common.base.Charsets;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriUtils;

import java.io.File;
import java.util.Objects;

import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;

/**
 * 基本Controller
 *
 * @author: ChenSH
 * @date: Create by in 17:44 2019-07-13
 */
public abstract class BaseController {
    /**
     * 返回成功和数据体
     *
     * @param data 数据体
     * @param <T>  数据泛型
     * @return 返回结果
     */
    public <T> Result<T> success(T data) {
        return Result.success(data);
    }

    /**
     * 返回成功和数据体
     *
     * @param <T> 数据泛型
     * @return 返回结果
     */
    public <T> Result<T> success() {
        return Result.success();
    }

    /**
     * 返回失败和错误描述,错误码为1
     *
     * @param <T> 数据泛型
     * @return 返回结果
     */
    public <T> Result<T> failure(String message) {
        return Result.failure(message);
    }

    /**
     * redirect跳转
     *
     * @param url 目标url
     * @return 跳转地址
     */
    public String redirect(String url) {
        return "redirect:".concat(url);
    }

    /**
     * 下载文件
     *
     * @param file 文件
     * @return {ResponseEntity}
     */
    public ResponseEntity<Resource> download(File file) {
        String fileName = file.getName();
        return download(file, fileName);
    }

    /**
     * 下载
     *
     * @param file     文件
     * @param fileName 生成的文件名
     * @return {ResponseEntity}
     */
    public ResponseEntity<Resource> download(File file, String fileName) {
        Resource resource = new FileSystemResource(file);
        return download(resource, fileName, APPLICATION_OCTET_STREAM);
    }

    public ResponseEntity<Resource> download(Resource resource) {
        return download(resource, resource.getFilename(), APPLICATION_OCTET_STREAM);
    }

    public ResponseEntity<Resource> download(Resource resource, MediaType contentType) {
        return download(resource, resource.getFilename(), contentType);
    }

    /**
     * 下载
     *
     * @param resource 资源
     * @param fileName 生成的文件名
     * @return {ResponseEntity}
     */
    public ResponseEntity<Resource> download(Resource resource, String fileName, MediaType contentType) {
        HttpHeaders headers = new HttpHeaders();
        if (Objects.isNull(contentType)) {
            contentType = APPLICATION_OCTET_STREAM;
        }
        headers.setContentType(contentType);
        String encodeFileName = UriUtils.encode(fileName, Charsets.UTF_8);
        // 兼容各种浏览器下载：
        // https://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/
        String disposition = "attachment;" +
                "filename=\"" + encodeFileName + "\";" +
                "filename*=utf-8''" + encodeFileName;
        headers.set(HttpHeaders.CONTENT_DISPOSITION, disposition);
        return new ResponseEntity<>(resource, headers, HttpStatus.OK);
    }

}
